﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MonoTouchAutoBind
{
    class BindProperty : MonotouchBind
    {
        /*
         
         @property(nonatomic,assign) id<FPPopoverControllerDelegate> delegate;
         
         */
        private string patternStart = @"@property\s*(\(\s*(?'Feature'[\w\s,]+)\s*\))?\s+(?'Retrun'[\w<>]+)\s*\*?\s*(?'propertyName'\w+)\s*;";
        private System.Text.RegularExpressions.Regex regex = null;


        private string propertyName, propertyType, FeatureNames;
        private bool isReadOnly = false;
        private bool isDelegate = false;

        public BindProperty()
            : base()
        {
            regex = new System.Text.RegularExpressions.Regex(patternStart);
            this.ParseFiniseStatus = ParseStatus.UnStart;
        }

        public override bool CanParse(string code)
        {
            return regex.IsMatch(code);
        }

        public override int PushCode(string code)
        {

            this.ParseFiniseStatus = ParseStatus.Processing;
            this.BindCode = code;
            if (this.CanParse(code))
            {
                System.Text.RegularExpressions.Match match = regex.Match(code);
                propertyName = match.Groups["propertyName"].Value.Trim();
                propertyType = match.Groups["Retrun"].Value.Trim();
                FeatureNames = match.Groups["Feature"].Value.Trim();

                isReadOnly = FeatureNames.Contains("readonly");
                isDelegate = propertyType.Contains("<") && propertyType.Contains(">");
                
                this.ParseFiniseStatus = ParseStatus.Finished;
                return match.Length;

            }
            else
            {
                this.ParseFiniseStatus = ParseStatus.Finished;
                DebugOut.Instance.Print(code);
                return 0;
            }
        }

        public override string GenerateCSharp()
        {

            StringBuilder sb = new StringBuilder();
            sb.Append("\t");
            sb.AppendFormat("//{0}", this.BindCode);
                sb.AppendLine();


            if (isDelegate)
            {
                //[Export("delegate")]
                //NSObject WeakDelegate { get; set; }

                //[Wrap("WeakDelegate")]
                //FPPopoverControllerDelegate Delegate { get; set; }

                string delegateType = this.propertyType.Substring(this.propertyType.IndexOf("<") + 1);
                delegateType = delegateType.Substring(0, delegateType.Length - 1);

                sb.Append("\t");
                sb.AppendFormat("[Export(\"{0}\")]", this.propertyName);
                sb.AppendLine();
                sb.Append("\t");
                sb.AppendFormat("NSObject Weak{0} {{ get; set; }}", this.propertyName);
                sb.AppendLine();
                sb.AppendLine();

                sb.Append("\t");
                sb.AppendFormat("[Wrap(\"Weak{0}\")]", this.propertyName);
                sb.AppendLine();
                sb.Append("\t");
                sb.AppendFormat("{0} {1} {{ get; set; }}", delegateType, this.propertyName);
                sb.AppendLine();


            }
            else
            {
                sb.Append("\t");
                sb.AppendFormat("[Export(\"{0}\")]", this.propertyName);
                sb.AppendLine();
                sb.Append("\t");
                sb.AppendFormat("{0} {1} {{ get;{2}}}", this.propertyType, this.propertyName, isReadOnly ? "" : " set; ");
                sb.AppendLine();
            }
            return sb.ToString();
        }

    }
}
